package com.warm.jpa.repository;


import com.warm.jpa.entity.BaseRoleUserEntity;
import com.warm.jpa.entity.BaseUserEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface BaseRoleUserRepository extends JpaRepository<BaseRoleUserEntity, Integer>{

    /**
     * 自定义SQL连表查询
     */
    @Query(value =
            "select  u " +
                    "from BaseRoleUserEntity ru " +
                    "left join com.warm.jpa.entity.BaseUserEntity u on ru.userAccount = u.account " +
                    "where ru.roleCode = : roleCode")
    List<BaseUserEntity> getUserList(@Param("roleCode")Integer roleCode);

    @Query(value =
            "select  u " +
                    "from BaseRoleUserEntity ru " +
                    "left join com.warm.jpa.entity.BaseUserEntity u on ru.userAccount = u.account " +
                    "where ru.roleCode in : list")
    List<BaseUserEntity> inQuery(List<Integer> list);

    @Query(value =
            "select  u " +
                    "from BaseRoleUserEntity ru " +
                    "left join com.warm.jpa.entity.BaseUserEntity u on ru.userAccount = u.account " +
                    "where ru.roleCode = :roleCode and u.age between :min and :max")
    List<BaseUserEntity> betweenQuery(int roleCode, int min, int max);
}

